<template>
  <div class="bonus">
    <dt-search :xlBtn="name == 'bill' ? 12 : 6" :xl="4" ref="search" :label-width="90" @reset="resetFun">
      <Button type="primary" v-if="unlocking('0112010100') && name == 'bill'" @click="onNew">
        新建奖金单
      </Button>
      <Button type="primary" v-if="name == 'bill'">
        <a
          href="https://images.tospurfang.com/doc/%E5%A5%96%E9%87%91%E5%8D%95%E6%93%8D%E4%BD%9C%E8%AF%B4%E6%98%8E.pdf"
          target="_blank"
          style="color: #fff">
          查看操作指引
        </a>
      </Button>
      <Button type="primary" v-if="needExtend" @click="onDefer">申请延期发起</Button>
      <Button type="primary" v-if="name == 'caseBillApproval'">
        <a
          href="https://images.tospurfang.com/doc/%E5%A5%96%E9%87%91%E5%8D%95%E5%AE%A1%E6%89%B9%E6%93%8D%E4%BD%9C%E6%8C%87%E5%BC%95.pdf"
          target="_blank"
          style="color: #fff">
          查看操作指引
        </a>
      </Button>
    </dt-search>

    <Tabs
      v-if="name == 'billApproval' || name == 'caseBillApproval'"
      type="card"
      :animated="false"
      @on-click="onTab"
      v-model="tabName">
      <TabPane label="待审批" name="0"></TabPane>
      <TabPane label="已通过" name="1"></TabPane>
      <TabPane label="已驳回" name="2"></TabPane>
    </Tabs>

    <dt-grid ref="table" page :onFetch="onFetch" />

    <new-modal ref="new" />
    <process-modal ref="process" />
    <defer-modal ref="defer" />
  </div>
</template>

<script>
  import Column from './opt/Column.js'
  import ApprovalColumn from './opt/ApprovalColumn.js'
  import SearchOpt from './opt/SearchOpt.js'

  import newModal from './components/new-modal.vue'
  import deferModal from './components/defer-modal.vue'
  import processModal from '@/components/biz/audit/process-modal.vue'

  import billMgr from '@/biz/bonus/bill'
  import auditMgr from '@/biz/flowApproval/processAudit'
  import extendMgr from '@/biz/bonus/bonusExtend.js'
  import dt from '@dt/dt'

  let instanceId = null

  export default {
    components: {
      newModal,
      processModal,
      deferModal
    },
    data() {
      let tabName = this.$route.params.tabCheck || '0'
      let name = this.$route.name
      let Col = name == 'bill' ? Column : ApprovalColumn
      return {
        tabName,
        name,
        column: new Col(name, tabName),
        needExtend: false,
        applyExtend: false
      }
    },
    watch: {
      $route(to, from) {
        let retainParams = dt.memory.get('billApprovalManager')
        if (to.name == 'billApprovalManager') {
          this.$refs.search.init(this.$refs.table, new SearchOpt(this.name), {
            state: retainParams?.state,
            housesId: retainParams?.housesId,
            companyId: retainParams?.companyId,
            housesName: retainParams?.housesName
          })
          this.$refs.table.fetch()
        }
      }
    },
    mounted() {
      let retainParams = dt.memory.get('billApprovalManager')
      instanceId = this.$route.params.businessId || instanceId

      this.$refs.table.init(this, this.column)
      this.$refs.search.init(this.$refs.table, new SearchOpt(this.name), {
        state: this.$route.name === 'billApprovalManager' ? retainParams?.state : null,
        housesId: retainParams?.housesId,
        companyId: retainParams?.companyId,
        housesName: retainParams?.housesName
      })
      this.$refs.table.fetch()
    },
    methods: {
      resetFun() {
        instanceId = null
        dt.memory.remove('billApprovalManager')
        this.$refs.search.init(this.$refs.table, new SearchOpt(this.name))
      },
      onFetch(data) {
        if (this.name == 'bill') {
          data.bonusType = data.bonusType || ''
          data.bonusMonth = data.bonusMonth || ''
          data.status = data.status || ''
          this.checkBonusExtend()
          return billMgr.list(data)
        } else {
          data.auditType = 3
          data.buttonType = this.name == 'billApprovalManager' ? -1 : Number(this.tabName)
          data.sortMethod = this.tabName == '0' ? 'asc' : 'desc'
          if (this.name == 'caseBillApproval') {
            data.housesId = this.$dt.buildInfo().housesId
            // 案场内待办任务跳转
            data.instanceId = instanceId
          }

          if (this.name == 'billApproval') {
            // 待办任务跳转
            data.instanceId = instanceId
          }

          if (this.name == 'billApprovalManager') {
            // 流程审批
            dt.memory.get('billApprovalManager')?.UContainSelf !== undefined &&
              (data.containSelf = !dt.memory.get('billApprovalManager'))
          }
          return auditMgr.list(data)
        }
      },
      checkBonusExtend() {
        return extendMgr.checkBonusExtend({ housesId: this.$dt.buildInfo().housesId }).then((res) => {
          this.needExtend = res.needExtend
          this.applyExtend = res.applyExtend
          return Promise.resolve(res)
        })
      },
      onTab(name) {
        this.$refs.table.init(this, new ApprovalColumn(this.name, name))
        this.$refs.table.fetch(true)
      },
      onNew() {
        this.$refs.new.show({})
      },
      onDefer() {
        this.checkBonusExtend().then((res) => {
          if (res.apply) {
            this.$Modal.info({
              title: '通知',
              content: '该项目已提交延期发起申请，请等待审批',
              okText: '关闭'
            })
          } else {
            if (res.needExtend) {
              this.$refs.defer.show({})
            } else {
              this.$Modal.info({
                title: '通知',
                content: '当前可直接发起奖金单，无需申请延期发起',
                okText: '关闭'
              })
            }
          }
        })
      },
      onDetail(item) {
        this.$router.push({
          path:
            this.name == 'bill'
              ? `bill/detail`
              : this.name == 'caseBillApproval'
              ? 'caseBillApproval/detail'
              : 'billApproval/detail',
          query: {
            id: this.name == 'bill' ? item.bonusId : item.businessId,
            type:
              this.name == 'bill'
                ? item.bonusType
                : this.$dt.dictInfo().__key('bonusType', item.auditData.bonusType)
          }
        })
      },
      onAudit(item) {
        billMgr.isCanApproveBonus(item.housesId).then((res) => {
          this.$router.push({
            path: this.name == 'caseBillApproval' ? 'caseBillApproval/detail' : 'billApproval/detail',
            query: {
              id: item.businessId,
              type: this.$dt.dictInfo().__key('bonusType', item.auditData.bonusType),
              auditId: item.auditId,
              housesId: item.housesId
            }
          })
        })
      },
      onEdit(item) {
        this.$router.push({
          path: `bill/edit`,
          query: {
            id: item.bonusId,
            type: item.bonusType
          }
        })
      },
      onResend(item) {
        billMgr.reSubmit(item.bonusId).then((res) => {
          this.$refs.table.fetch()
          this.$router.push({
            path: `bill/edit`,
            query: {
              id: res,
              type: item.bonusType
            }
          })
        })
      },
      onRecall(item) {
        this.$Modal.confirm({
          title: '是否确认撤回该奖金单？',
          onOk: () => {
            auditMgr
              .back({
                auditType: 3,
                businessId: item.bonusId
              })
              .then((res) => {
                this.$refs.table.fetch()
              })
          }
        })
      },
      onFlow(item) {
        if (this.name == 'bill') {
          this.$refs.process.show({
            auditType: 3,
            businessId: item.bonusId
          })
        } else {
          this.$refs.process.show({
            ...item,
            auditType: 3
          })
        }
      },
      onDelete(item) {
        this.$Modal.confirm({
          title: '是否确认删除该奖金单？',
          onOk: () => {
            billMgr.del(item.bonusId).then((res) => {
              this.$refs.table.fetch()
            })
          }
        })
      },
      onSkip(item) {
        this.$Modal.confirm({
          title: '请确认是否跳过该审批节点？',
          onOk: () => {
            auditMgr
              .audit({
                auditId: item.auditId,
                auditResultFlag: 1,
                isSkip: item.isSkip
              })
              .then((res) => {
                this.$Message.success('操作成功')
                this.$refs.table.fetch()
              })
          }
        })
      },
      onRecalculate(item) {
        this.$Modal.confirm({
          title: '是否要重新计算该奖金单？',
          onOk: () => {
            billMgr.reCalculate(item.bonusId).then((res) => {
              this.$refs.table.fetch()
              this.$router.push({
                path: `bill/edit`,
                query: {
                  id: res,
                  type: item.bonusType
                }
              })
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bonus {
    padding: 20px;
  }
</style>
